#include <QtCore/QString>
#include <QtCore/QList>
#include <QDebug>
#include <QtDBus>
#include "fusionnotify.h"

#ifndef VER
#define VER "0.0.0"
#endif

class FusionNotify::FNotifyImpl
{
public:
    FNotifyImpl() : Notifier(new QDBusInterface("org.freedesktop.Notifications",
                                                "/org/freedesktop/Notifications", "org.freedesktop.Notifications"))
    {}

    // See http://www.galago-project.org/specs/notification/0.9/x408.html
    QDBusReply<unsigned int> notify(QString name, QString icon, QString summary, int timeout = -1,
                                    uint replaces = 0, QString body = QString(),
                                    QStringList actions = QStringList(),
                                    QMap<QString, QVariant> *hints = new QMap<QString, QVariant>)
    {
        QList<QVariant> args;
        args.append(name);
        args.append(replaces);
        args.append(icon);
        args.append(summary);
        args.append(body);
        args.append(actions);
        args.append(*hints);
        args.append(timeout);

        QDBusReply<unsigned int> reply = Notifier->callWithArgumentList(QDBus::Block, "Notify", args);
        QDBusError err = Notifier->lastError();
        if(err.isValid())
        {
            qDebug() << err.name();
            qDebug() << err.message();
        }
        delete hints;
        return reply;

    }

    QString version()
    {
        return QString(VER);
    }

private:
    QDBusInterface * Notifier;
};

FusionNotify::FusionNotify() : p(new FusionNotify::FNotifyImpl())
{}

FusionNotify::~FusionNotify()
{
    delete p;
}

void FusionNotify::notify(QString name, QString icon, QString summary, int timeout,
                                              int replaces, QString body, QStringList actions,
                                              QMap<QString, QVariant> *hints)
{
    p->notify(name, icon, summary, timeout, replaces, body, actions, hints);
}

QString FusionNotify::version()
{
    return p->version();
}
